package cn.ac.cintcm.prespring.cluster.set;

import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.solr.core.Config;

public class DataPreHandle {
	private static String driver = "oracle.jdbc.driver.OracleDriver";

	private static String url = "jdbc:oracle:thin:@192.168.200.51:1521:dell";

	private static String username = "tcm_ncdi";

	private static String password = "ncdi51";

	private static Connection conn;

	public static void connect() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (ClassNotFoundException e) {
			System.out.println("driver not found!");
			System.exit(1);
		} catch (SQLException e) {
			System.out.println("SQL EXCEPTION WHEN CONNECTING:");
			e.printStackTrace();
		}
	}

	public static void outputToText() throws Exception{
		connect();
		Statement stmt = conn.createStatement();
		String sql = "select FJ_ID, ZUZ_ID from P_ZZ_FJ order by FJ_ID, ZUZ_ID";
		ResultSet rset = stmt.executeQuery(sql);
		String previous = "first";
		String current = "";
		BufferedWriter writer = new BufferedWriter(
                new OutputStreamWriter(new FileOutputStream(Config.getInstanceDir()+ "data/originalData/PRES_FJ_ZYS.txt",false),"UTF8"));
		StringBuilder sb = new StringBuilder();
		while (rset.next()) {
			current = rset.getString("FJ_ID");
			String hb = rset.getString("ZUZ_ID");
			if (current.equals(previous) || previous.equals("first")) {
				if (previous.equals("first")) {
					sb.append(current);
					sb.append(":");
				}
				sb.append(hb);
				sb.append(",");
				previous = current;
			} else {
				if (sb.length() > 0) {
					sb.deleteCharAt(sb.length() - 1);
				}
				System.out.println(sb.toString());
				writer.write(sb.toString());
				writer.write("\r\n");
				sb.setLength(0);

				sb.append(current);
				sb.append(":");
				sb.append(hb);
				sb.append(",");
				previous = current;
			}
		}
		rset.close();
		stmt.close();
		writer.close();
	}
	
	public static void outputFjNameToText() throws Exception {
		connect();
		Statement stmt = conn.createStatement();
		String sql = "select ID,FANGM  from pres_zyfj t group by t.fangm,t.id";
		ResultSet rset = stmt.executeQuery(sql);
		String previous = "first";
		String current = "";
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(Config.getInstanceDir()+ "data/originalData/PRES_FJ_Name.txt", false),"UTF8"));
		StringBuilder sb = new StringBuilder();
		while (rset.next()) {
			current = rset.getString("FANGM");
			String fjid = rset.getString("ID");
			if (current.equals(previous) || previous.equals("first")) {
				if (previous.equals("first")) {
					sb.append(current);
					sb.append(":");
				}
				sb.append(fjid);
				sb.append(",");
				previous = current;
			} else {
				if (sb.length() > 0) {
					sb.deleteCharAt(sb.length() - 1);
				}
				System.out.println(sb.toString());
				writer.write(sb.toString());
				writer.write("\r\n");
				sb.setLength(0);

				sb.append(current);
				sb.append(":");
				sb.append(fjid);
				sb.append(",");
				previous = current;
			}
		}
		rset.close();
		stmt.close();
		writer.close();
	}
	
	public static void main(String[] args) throws Exception{
		outputToText();
		//int times=3567/1000;
		//System.out.print(times);//=3
	}
}
